<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
	<title></title>
	<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
	<link rel="stylesheet" type="text/css" href="../../css/font-awesome.css" />
</head>

<body>
	<div class="aui-content">
		<div class="aui-content aui-margin-t-15">
			<img src="../../image/shuakaqu.png" />
		</div>
		<div class="aui-padded-15 aui-text-danger aui-text-center"><b>请将二代身份证贴近机器背面的读卡区域</b></div>
	</div>
</body>
<script id="idCardTemplate" type="text/x-dot-template">
	<ul class="aui-list aui-media-list" id="list">
		<li class="aui-list-item">
			<div class="aui-media-list-item-inner">
				<div class="aui-list-item-media">
					<img src="data:image/png;base64,{{=it.photo}}" id="idcard_img" />
				</div>
				<div class="aui-list-item-inner">
					<div class="aui-list-item-text">
						<div class="aui-list-item-title">
							<input type="hidden" value="{{=it.name}}" id="name" />
							<input type="hidden" value="{{=it.sex}}" id="sex" /> {{=it.name}}({{=it.sex}})
						</div>
					</div>
					<div class="aui-content">
						<p>
							<input type="hidden" value="{{=it.nation}}" id="nation" /> 民族：{{=it.nation}}
						</p>
						<p>
							<input type="hidden" value="{{=it.birthday}}" id="birthday" /> 出生日期：{{=it.birthday}}
						</p>
						<p>
							<input type="hidden" value="{{=it.address}}" id="address" /> 地址：{{=it.address}}
						</p>
						<p>
							<input type="hidden" value="{{=it.idcard}}" id="idcard" /> 身份证号：{{=it.idcard}}
						</p>
						<p>
							<input type="hidden" value="{{=it.authority}}" id="agency" /> 签发机关：{{=it.authority}}
						</p>
						<p>
							<input type="hidden" value="{{=it.startdate}}" id="validstartdate" />
							<input type="hidden" value="{{=it.enddate}}" id="validenddate" /> 有效期限：{{=it.startdate}}-{{=it.enddate}}
						</p>
					</div>
				</div>
			</div>
		</li>

		<li class="aui-list-item">
			<div class="aui-list-item-inner aui-list-item-center aui-list-item-btn">
				<div class="aui-btn aui-btn-info aui-btn-block" onclick="save()" tapmode>
					<i class="fa fa-address-card"></i> 身份数据上报
				</div>
		</li>

	</ul>
</script>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/doT.js"></script>
<script type="text/javascript" src="../../script/util.js"></script>
<script type="text/javascript">
	var idcard;
	var td;
	var i = 0;
	var sourceType = "camera";
	apiready = function() {
		var cardid = api.pageParam.id;
		idcard = api.require('moduleBoyaIDCard');
		td = api.require('talkingData');
		sourceType = api.deviceId=="359261051214885"?"album":"camera";
		sourceType = api.deviceModel =="iphone"?"album":"camera";

		//console.log(api.deviceId);
		// 开启供电&服务
		if (idcard) {
			idcard.openGPIO();
			idcard.startRead();
		} else {
			tips('只支持【青鸟一体化设备】读取');
		}

		api.parseTapmode();
		var sint = setInterval(function() {
			var ret = null;
			i++;
			if (idcard) {
				ret = idcard.ReadCard();
				console.log("身份证数据：" + JSON.stringify(ret));
			}
			if (i > 90) {
				clearInterval(sint);
				api.hideProgress();
				api.confirm({
					title: '提示',
					msg: '长时间未识别到身份证，是否拍照认证？',
					buttons: ['确定', '取消']
				}, function(ret, err) {
					if (ret && ret.buttonIndex === 1) {
						api.getPicture({
							sourceType: sourceType,
							encodingType: 'jpg',
							mediaValue: 'pic',
							destinationType: 'url',
							quality: 100,
							targetWidth: 480,
							targetHeight: 800,
							saveToPhotoAlbum: false
						}, function(ret, err) {
							if (ret && ret.data != "") {
								//alert(JSON.stringify(ret));
								api.openWin({
								    name: 'browse_card_photo',
								    url: api.wgtRootDir + '/html/card/photo.html',
								    pageParam: {
								        id: api.pageParam.id,
												photo: ret.data
								    }
								});

							} else {
								api.alert({
									msg: '请拍照后再进行认证'
								}, function(ret, err) {
									//coding...
									api.closeWin({});
								});
							}
						});

					} else {
						api.closeWin({});

					}
				});

			}
			if (ret && ret.code === -3) {
				api.showProgress({
					title: '正在读卡'
				});
			} else if (ret && ret.code === 1) {
				//仅提示音
				api.notification({
					sound: 'default'
				});
				clearInterval(sint);
				if (cardid == ret.idcard) {
					td.onEvent({
						eventId: '身份认证',
						eventLabel: '认证结果',
						parameters: {
							content: $api.getStorage('userinfo').user.userAccount + "-" + $api.getStorage('userinfo').user.geroShortName + "-" + ret.idcard + "-" + true + "-" + new Date().format("yyyy-MM-dd hh:mm:ss")
						}
					});
					var template = $api.byId('idCardTemplate');
					var pagefn = doT.template(template.text);
					$api.html(document.body, pagefn(ret));
					idcard.stopRead();
				} else {
					idcard.stopRead();
					td.onEvent({
						eventId: '身份认证',
						eventLabel: '认证结果',
						parameters: {
							content: $api.getStorage('userinfo').user.userAccount + "-" + $api.getStorage('userinfo').user.geroShortName + "-" + ret.idcard + "-" + false + "-" + new Date().format("yyyy-MM-dd hh:mm:ss")
						}
					});
					api.alert({
						msg: '证件信息与该老人信息不匹配!'
					}, function(ret, err) {
						//coding...
						console.log(JSON.stringify(ret));
						api.closeWin({});
					});
				}
				api.hideProgress();
			}
		}, 500);
	};

	// 接收数据
	// 打开身份证识别的APP
	//						api.addEventListener({
	//							name : 'appintent'
	//						}, function(ret, err) {
	//							console.log(JSON.stringify(ret));
	//							if (ret && ret.appParam) {
	//								console.log(cardid);
	//								console.log(ret.appParam.idcard);
	//								if (cardid == ret.appParam.idcard) {
	//									var template = $api.byId('idCardTemplate');
	//									var list_content = $api.byId('list');
	//									var pagefn = doT.template(template.text);
	//									$api.html(list_content, pagefn(ret));
	//								} else {
	//									api.alert({
	//										msg : '证件信息与该老人信息不匹配!'
	//									}, function(ret, err) {
	//										//coding...
	//										console.log(JSON.stringify(ret));
	//										api.closeWin({
	//										});
	//									});
	//								}
	//							}
	//						});

	function save() {
		var idcard_img = $api.attr($api.byId('idcard_img'), 'src');
		var name = $api.val($api.byId('name'));
		var idcard = $api.val($api.byId('idcard'));
		var nation = $api.val($api.byId('nation'));

		console.log(name);
		console.log(idcard);
		console.log(idcard_img.substring(idcard_img.indexOf(",") + 1));
		//			trans.decodeImgToBase64({
		//				imgPath : idcard_img
		//			}, function(ret, err) {
		//				if (ret.status) {
		api.showProgress({});
		api.ajax({
			url: base + 'gero/api/nurseTerminal/idCardInfoCollect/saveIdCardInfoCollect',
			method: 'post',
			data: {
				values: {
					"imgBase64": idcard_img.substring(idcard_img.indexOf(",") + 1),
					"olderIdCardNumber": idcard,
					"olderName": name,
					"token": $api.getStorage('userinfo').token,
					"olderNation": nation
				}
			}
		}, function(ret, err) {
			console.log(JSON.stringify(ret));
			if (ret && ret.code == "200") {
				api.alert({
					msg: '数据上报成功'
				}, function(ret, err) {
					//coding...
					console.log(JSON.stringify(ret));
					var jsfun = 'reashlist();';
					api.execScript({
						name: 'browse_oldlist',
						frameName: 'browser',
						script: jsfun
					});

					api.closeWin({});
					api.hideProgress();
				});
			} else if (ret && ret.code == 'ERROR') {
				api.alert({
					msg: ret.msg
				}, function(ret, err) {
					//coding...
					console.log(JSON.stringify(ret));

					api.closeWin({});
					api.hideProgress();
				});
			} else {
				doLogin();
			}
		});
		//				} else {
		//					api.alert({
		//						msg : err
		//					}, function(ret, err) {
		//						//coding...
		//						console.log(JSON.stringify(ret));
		//						api.closeWin({
		//						});
		//					});
		//				}
		//			});
		//var idcard = $api.attr($api.byId('idcard'), 'src');
	}
</script>

</html>
